using System.Collections;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    [Header("数据")]
    public EnemyData data;
    [Header("是否需要对话")]
    public bool needSay = false;
    public SayData sayData;
    [Header("碰撞体")]
    public GameObject collObj = null;
    [Header("材质")]
    public Material oriMat;
    [Header("最后勇士死亡的对话")]
    public SayData dieData;
    [Header("最后的勇者")]
    public Enemy finalEnemy;
    public float hpFinalTimer = 1f;
    public float hpFinalStopTimer = 1.5f;
    private void Awake()
    {
        //设置雕像
        SetSculpture();

        //获取碰撞体
        if(data.collSize==1) collObj = transform.Find("CheckPosOne").gameObject;
        else if(data.collSize==2) collObj = transform.Find("CheckPosTow").gameObject;
        else if(data.collSize==3) collObj = transform.Find("CheckPosThree").gameObject;

        //隐藏
        collObj.GetComponent<Collider2D>().enabled = false;

        //获取材质
        oriMat = transform.Find("Sprite").GetComponent<SpriteRenderer>().material;
    }
    private void OnEnable()
    {
        GetComponent<Collider2D>().enabled = true;
    }
    private void Update()
    {
        //给骑士回血
        HpToFinal();
    }
    //初始化
    public void OnInit()
    {
        if(GameManager.Instance.isBloodMoon)
        {
            //血月设置
            BloodOfMoonInit();
        }
        else
        {
            ReduceModify();
        }
    }
    //退出
    public void OnClose()
    {
        //只有是血月才会退出减少
        if(GameManager.Instance.isBloodMoon)
        {
            //减少增益
            ReduceModify();
        }
    }
    //设置雕像
    public void SetSculpture()
    {
        //雕像
        if (data.isSculp)
        {
            if (data.isActive)
            {
                transform.Find("Sprite").GetComponent<SpriteRenderer>().enabled=false;
            }
            else
            {
                transform.Find("Sprite/Sculpture").gameObject.SetActive(false);
                transform.Find("Sprite").GetComponent<SpriteRenderer>().enabled = true;
            }
        }
    }
    //计算受伤
    public void BeHurt(int amount,bool isAud)
    {
        data.currHp -= amount;
        data.oriHp -= amount;
        //伤害数字
        GameManager.Instance.CreateHurtDamage(amount, transform.position);
        if (data.currHp<=0)
        {
            Die();
        }
        else
        {
            //受伤
            StartCoroutine(BeHurtEffect());
        }

        if(isAud)
        {
            AudManager.instance.PlayEnemyAud(AudManager.instance.enemyDic["受伤"], 1f);
        }
    }
    //设置坐标为可行走
    public void SetPosRun()
    {
        //获取Coll的子物体
        GameObject obj = transform.Find("Coll").gameObject;
        for(int i=0;i<obj.transform.childCount;i++)
        {
            Vector2 objPos = obj.transform.GetChild(i).position;
            Vector2Int pos = new Vector2Int(Mathf.RoundToInt(objPos.x),
            Mathf.RoundToInt(objPos.y));
            AStarManager.Instance.SetRun(pos.x, pos.y);
        }
        //应用到玩家
        AStarManager.Instance.SetAllAStar();
    }
    //设置坐标为Enemy
    public void SetPosEnemy()
    {
        //获取Coll的子物体
        GameObject obj = transform.Find("Coll").gameObject;
        for (int i = 0; i < obj.transform.childCount; i++)
        {
            Vector2 objPos = obj.transform.GetChild(i).position;
            Vector2Int pos = new Vector2Int(Mathf.RoundToInt(objPos.x),
            Mathf.RoundToInt(objPos.y));
            AStarManager.Instance.SetEnemy(pos.x, pos.y);
        }
        //应用到玩家
        AStarManager.Instance.SetAllAStar();
    }
    //打开碰撞体
    public void OpenColl()
    {
        //关闭点击敌人
        SceneItemManager.instance.clickEnemyRight = true;
        collObj.GetComponent<Collider2D>().enabled=true;
        SceneItemManager.instance.SetClickEnemy(gameObject);
    }
    //关闭碰撞体
    public void CloseColl()
    {
        collObj.GetComponent<Collider2D>().enabled = false;
    }
    //死亡
    public void Die()
    {
        //生成特效
        GameObject obj = PoolManager.Instance.GetNormalDieEffect();
        obj.transform.position = transform.position;
        //关闭自身
        gameObject.SetActive(false);

        if (transform.parent.GetComponent<CircleObj>()==null)
        {
            if(data.myName!="怨灵")
            {
                data.isDie = true;
                //移除敌人
                SceneItemManager.instance.ReEnemy(transform);
                //设置自身可以行走
                SetPosRun();
            }
        }
        //掉落金币
        DropCoin();
        //掉落怨灵珠
        DropCircle();
        //掉落神器
        if(data.myName== "狼蛛")
        {
            ArtifactManager.instance.AddArtifact("撒钱手");
        }
        //终勇死亡
        if(data.myName=="最后的骑士")
        {
            //PanelManager.Open<SayPanel>(dieData);

            AchiManager.instance.UnLockAchi("勇士，毁灭");
        }
        else if(data.myName=="怨灵")
        {
            AchiManager.instance.CheckNumAchi("怨灵出现");
            AchiManager.instance.CheckNumAchi("怨灵杀手");
        }
        else if (data.myName == "迷失史莱姆")
        {
            GameManager.Instance.ChangeRedTurn(1);

            TipSayManager.Instance.Say("恭喜获得<color=yellow> 红符 </color>");
        }
        else if(data.myName== "魔化公主")
        {
            //GameManager.Instance.ChangeStone(1);
            //TipSayManager.Instance.Say("获取 <color=yellow> 超脱石 + 1</color>");

            //提示可以从0层进入花园
            TipSayManager.Instance.Say("<color=red> 某个存在 </color>的力量增强了");
            GameManager.Instance.isKillPro = true;
        }
        else
        {
            //AudManager.instance.PlayEnemyAud(AudManager.instance.enemyDic["死亡"], 1f);
        }
    }
    //点击
    private void OnMouseDown()
    {
        //打开碰撞体
        OpenColl();
    }
    //掉落金币
    public void DropCoin()
    {
        float currCoin = data.dropCoin;

        //如果拥有撒钱手,撒钱手对怨灵不生效
        if(data.myName!="怨灵")
        {
            if (ArtifactManager.instance.SeekArtifact("撒钱手")) currCoin*=1.5f;
        }
        //这里有其他条件
        string str = "你杀死了<color=red>" + data.myName + "</color>,掉落了 <color=yellow>" +
        "金币</color> X " + currCoin;
        TipSayManager.Instance.Say(str);

        //增加金币
        GameManager.Instance.AddCoin(currCoin);
    }
    //掉落怨灵珠
    public void DropCircle()
    {
        if (data.dropCircle <= 0) return;

        int currCoin = data.dropCircle;

        //这里有其他条件
        string str = "你杀死了<color=red>" + data.myName + "</color>,掉落了 <color=yellow>" +
        "怨灵珠</color> X " + currCoin;
        TipSayManager.Instance.Say(str);

        //增加金币
        GameManager.Instance.ChangeCircle(currCoin);
    }
    //被伤害
    public IEnumerator BeHurtEffect()
    {
        transform.Find("Sprite").GetComponent<SpriteRenderer>().material = PoolManager.Instance.hurtMat;

        yield return new WaitForSeconds(0.15f);

        transform.Find("Sprite").GetComponent<SpriteRenderer>().material = oriMat;
    }
    //血月
    public void BloodOfMoonInit()
    {
        //所有属性翻倍
        if(GameManager.Instance.isBloodMoon)
        {
            data.currHp = data.oriHp*2;
            data.damage = data.oriDamage * 2;
            data.armor = data.oriArmor * 2;
            data.dropCoin = data.oriDropCoin * 2;
            data.dropCircle = data.oriDropCircle * 2;
        }
    }
    //属性减少
    public void ReduceModify()
    {
        data.currHp = data.oriHp;
        data.damage = data.oriDamage;
        data.armor = data.oriArmor;
        data.dropCoin = data.oriDropCoin;
        data.dropCircle = data.oriDropCircle;
    }
    //回血
    public void AddHp(int amount)
    {
        data.currHp += amount;

        GameManager.Instance.CreateAddHpNum(amount, transform.position);
    }
    //给勇者回血
    public void HpToFinal()
    {
        if(finalEnemy!=null&&GameManager.Instance.isBattle&&SceneItemManager.instance.clickEnemy.GetComponent<Enemy>().data.myName== "最后的骑士")
        {
            hpFinalTimer -= Time.deltaTime;
            if(hpFinalTimer<=0)
            {
                finalEnemy.AddHp(150);
                hpFinalTimer = hpFinalStopTimer;
            }
        }
    }
    //设置初始数据
    public void SetOriData()
    {
        data.oriDamage = data.damage;
        data.oriArmor = data.armor;
        data.oriHp = data.currHp;
        data.oriDropCoin = data.dropCoin;
        data.oriDropCircle = data.dropCircle;
    }
}